<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
class HuodongController extends AdminbaseController{



	function _initialize() {
		parent::_initialize();
		$this->db = D("Huodong");

	}

	function index(){



		if(I("get.keyword")){

			$map['name']=array("like","%".trim(I("get.keyword"))."%");
			$map['tel']=array("like","%".trim(I("get.keyword"))."%");
			$map['lianxiren']=array("like","%".trim(I("get.keyword"))."%");
			$search=true;

		}

		$this->assign("search",$search);




		$count=$this->db->where($map)->count();
		$page = $this->page($count);
		$list=$this->db->limit($page->firstRow . ',' . $page->listRows)->order ( "id desc")->where($map)->select();


		foreach($list as $k=>$v){
			$canyu=M("Order")->where(array("gl_id"=>$v["id"],"modal"=>"huodong"))->count();

			$list[$k]["canyu"]=$canyu;
			$list[$k]["shengyu"]=$v["kucun"]-$canyu*$v["xian_num"];



		}


		$this->assign("Page",$page->show(""));




		$this->assign("list",$list);

		$this->display();
	}



	function canyu (){



		$id = intval(I("get.huodong_id"));

		if(!$id){
			$this->error("缺少参数！");
		}else{
			$map["gl_id"]=$id;
			$map["modal"]="huodong";
		}


		$list=D("Order")->where($map)->select();



		$this->assign("_list",$list);


		$this->display();
	}


	function add(){

		if(IS_POST){

			if ($this->db->create($_POST)) {
				if ($this->db->add()!==false) {

//					echo "添加成功";

					$this->success("添加成功！", U("index"));
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->db->getError());
			}
		}else{



			$this->display("form");
		}
	}


	function edit(){


		if(IS_POST){
			if ($this->db->create($_POST)) {

				if ($this->db->save()!==false) {

					$this->db->data_cache();
					$this->success("保存成功！");
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->db->getError());
			}

		}else{

			$id= intval(I("get.id"));
			if(!$id){
				$this->error("缺少参数！");
			}

			$r=$this->db->where("id=$id")->find();
			$this->assign("edit",true);
			$this->assign("data",$r);
			$this->display("form");
		}
	}


	function  qianshou(){



	}



}